package academic.ui.server.utils;

import java.util.List;

import org.apache.torque.TorqueException;
import org.apache.torque.util.BasePeer;

import torque.generated.Creditcard;
import torque.generated.CreditcardPeer;

import academic.ui.client.util.KKGWTException;
import academic.ui.client.util.KKGWTInvalidSessionException;
import ecommerce.soco.shared.GWT_CreditCard;

import com.workingdogs.village.DataSetException;
import com.workingdogs.village.Record;

public class QueryCardDetails {
	
	
	
	/**
	 * Insert the new credit card into the database (payment_details table)
	 * @param card
	 * @return
	 */
	public static Integer insertCard(GWT_CreditCard card) {
		try {
			
			BasePeer.executeStatement("insert into payment_details (customers_id,ccOwner,billing_address,billing_zip,cc_number,cc_expires,billing_city," +
					"billing_state,last_used,cc_type) values (" +
					+card.getCustomersId()+",'"+card.getCcOwner()+"','"+card.getBillingAddress()+"','"+card.getBillingZip()+"','"+card.getCcNumber()+
					"','"+card.getCcExpires()+"','"+card.getBillingCity()+"','"+card.getBillingState()+"',now(),'"+card.getCcType()+"')");
			
		} catch (TorqueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
		return 1;
	}
	
	public static Boolean isCustomerCCNumberSaved(GWT_CreditCard card) {
		try {
			return !BasePeer.executeQuery("select payment_details_id from payment_details where " +
					"customers_id="+card.getCustomersId()+" and cc_number='"+card.getCcNumber()+"'").isEmpty();
		} catch (TorqueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}
